package com.example.stub.boot;

import static java.lang.System.out;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.example.stub.SimulatorClient;
import com.example.stub.SimulatorThread;
import com.example.stub.simulator.ManagerRegisterThread;
import com.example.stub.simulator.PackageThread;
import com.example.stub.simulator.TerminalRegisterThread;

public class PerformanceTestClient {

	private static final int THREAD_COUNT = 100;
	private static final int INTERVAL = 400;
	private static final int TOTAL_PACKAGE = 1000;
	
	public static void main(String[] args) throws Exception {
		if(ArrayUtils.isEmpty(args) || args.length < 1){
			printHelp();
			return;
		}
		
		String tcase = args[0];
		String v = getParamValue(args, "-c");
		int threadCount = StringUtils.isBlank(v) ? THREAD_COUNT : Integer.valueOf(v);
		
		v = getParamValue(args, "-i");
		int interval = StringUtils.isBlank(v) ? INTERVAL : Integer.valueOf(v);
		
		v = getParamValue(args, "-a");
		final int packageCount = StringUtils.isBlank(v) ? TOTAL_PACKAGE : Integer.valueOf(v);
		
		SimulatorClient client = null;
		
		if("-mr".equals(tcase)){
			// M端注册压力测试
			client = new SimulatorClient(threadCount, interval){
				@Override
				protected SimulatorThread createSimulatorThread(int no, int totalThread) throws Exception {
					return new ManagerRegisterThread(no, totalThread);
				}
			};
			
		}else if("-tr".equals(tcase)){
			// T端注册压力测试
			client = new SimulatorClient(threadCount, interval){
				@Override
				protected SimulatorThread createSimulatorThread(int no, int totalThread) throws Exception {
					return new TerminalRegisterThread(no, totalThread);
				}
			};
		}else if("-p".equals(tcase)){
			// 丢包率测试
			client = new SimulatorClient(threadCount, interval){
				@Override
				protected SimulatorThread createSimulatorThread(int no, int totalThread) throws Exception {
					return new PackageThread(no, totalThread, packageCount);
				}
			};
		}else if("-s".equals(tcase)){
			// 中转服务器压力测试
			client = new SimulatorClient(threadCount, interval){
				@Override
				protected SimulatorThread createSimulatorThread(int no, int totalThread) throws Exception {
					return new PackageThread(no, totalThread, 0);
				}
			};			
		}else{
			printHelp();
			return;
		}
		
		client.execute();
	}

	static String getParamValue(String[] args, String prefix){
		String value = null;
		
		for(String s : args){
			if(s.contains(prefix)){
				value = s.substring(prefix.length());
				break;
			}
		}
		
		return value;
	}
	
	static void printHelp() {
		out.println("默认线程数100，启动间隔400毫秒");
		out.println("M端注册压力测试");
		out.println(" -mr [-c线程数] [-i线程启动间隔毫秒]");
		out.println("T端注册压力测试");
		out.println(" -tr [-c线程数] [-i线程启动间隔毫秒]");
		out.println("丢包率测试");
		out.println(" -p [-c线程数] [-i线程启动间隔毫秒] [-a数据包总数，默认1000]");
		out.println("中转服务器压力测试");
		out.println(" -s [-c线程数] [-i线程启动间隔毫秒]");
		out.println("调用例子：-mr -t100 -i500");
	}
	
}
